import Base from "../Base.js";
import adminsApi from "../../api/admins.js";
export default class extends Base {
    constructor(props) {
        super(props);
    }
    handle() {
        layui.table.on('tool(adminsList)', async (obj) => {
            //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            // var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            // var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）
            const _id = obj.data._id;
            if (layEvent === 'edit') {
                const data = JSON.parse(sessionStorage.getItem("adminInfo"));
                if (data._id != _id) {
                    layui.layer.msg("您无法更改其他管理员");
                    return;
                }
                let index = layui.layer.open({
                    title: "信息修改",
                    type: 1,
                    content: `
                    <form class="layui-form" action="put" style="margin-right:55px;margin-top:20px;">
                        <div class="layui-form-item">
                            <label for="user" class="layui-form-label">用户名</label>
                            <div class="layui-input-block">
                                <input type="text" name="adminName" required lay-verify="required"value="${obj.data.adminName}"
                                    autocomplete="off" class="layui-input" id="user">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label for="pwd" class="layui-form-label">密&nbsp;&nbsp;&nbsp;&nbsp;码</label>
                            <div class="layui-input-block">
                                <input type="password" name="adminPassword" required lay-verify="required" placeholder="请输入新密码"
                                    autocomplete="off" class="layui-input" id="pwd">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-input-block" style="margin-left: 55px;">
                                <input type="button" value="修改" class="layui-btn" lay-filter="update" id="update"
                                    style="width: 100%;border-radius:19px" ;>
                            </div>
                        </div>
                    </form>`,
                });
                $("#update").on("click", async e => {
                    obj.data.adminName = $("input[name=adminName]").val();
                    obj.data.adminPassword = $("input[name=adminPassword]").val();
                    console.log(obj.data);
                    await adminsApi.update(obj.data);
                    layer.close(index);
                });
            }
            else if (layEvent === 'loginDate') {
                layui.layer.open({
                    title: "登录时间",
                    type: 1,
                    area: ['300px', '300px'],
                    content: `${obj.data.loginDate.map(item => `<div style="text-align:center;margin-bottom:10px">${item}</div>`).join("")}`,
                });
            }
        });
    }
    render() {
        this.el.html(`
            <table id="adminsList" lay-filter="adminsList"></table>
            <script type="text/html" id="barDemo">
                <a class="layui-btn layui-btn-xs" lay-event="loginDate">查看登录时间记录</a>
                <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
            </script>
        `);
    }
    mounted() {
        this.reRender();
    }
    reRender() {
        layui.table.render({
            elem: '#adminsList'
            , method: "get"
            , url: '/api/admins' //数据接口
            , headers: { Authorization: `Bearer ${adminsApi.getToken()}` }
            , page: true //开启分页
            , limits: [5, 10, 15, 20]
            , cols: [[ //表头
                { field: '_id', title: 'ID', sort: true, fixed: 'left' }
                , { field: 'adminName', title: '账号' }
                , { field: 'adminPassword', title: '密码', sort: true }
                , { fixed: 'right', align: 'center', toolbar: '#barDemo' }
            ]],
        });
    }
}